package com.uaniheng.util.matcher;

import java.util.Map;

/**
 * @author gyc
 */
class BaseCondition implements Condition {

    /**
     * 基本判断条件
     */
    private static final ElementEnum STATUS = ElementEnum.BASE_CONDITION;

    private final String label;
    private final String value;
    private final String operation;

    public BaseCondition(String label, String value, String operation) {
        this.label = label;
        this.value = value;
        this.operation = operation;
    }

    /**
     * 基本label和value字符串的关系判断逻辑
     * @param map
     * @return
     */
    @Override
    public boolean match(Map<String, String> map) {
        if (!map.containsKey(label)){
            return false;
        }
        switch (operation){
            case "=":
                if (value == null){
                    return map.get(label) == null;
                }else {
                    return value.equals(map.get(label));
                }
            case "!=":
                if (value == null){
                    return map.get(label) != null;
                }else {
                    return !value.equals(map.get(label));
                }
            case "like":
                if (value == null){
                    throw new RuntimeException("like null");
                }else {

                    return map.get(label).contains(value);
                }
            default:
                return false;
        }

    }

    @Override
    public ElementEnum getStatus() {
        return STATUS;
    }
}
